package com.cookmenu.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.cookmenu.domain.Food;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxParams;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends FinalActivity {
	// Button v;
	@ViewInject(id = R.id.et_search)
	EditText et_search;
	@ViewInject(id = R.id.btn_search)
	Button btn_search;
	@ViewInject(id = R.id.tv_searchresult)
	TextView tv_searchresult;
	@ViewInject(id=R.id.iv_searchresult)
	ImageView iv_searchresult;
	
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Food food = (Food) msg.obj;
				FinalBitmap bp  = FinalBitmap.create(MainActivity.this);
				String img = food.getAlbums().toString().substring(2, food.getAlbums().toString().length()-2);
				img = img.replace("\\", "");
				bp.display(iv_searchresult, img.toString());
				System.out.println(img.toString());
//				iv_searchresult.setImageBitmap(bp);
				tv_searchresult.setText("菜名："+food.getTitle()+ "\n"
						+ "标签："+food.getTags()+ "\n" + "介绍："+food.getImtro()
						+ "\n" + "材料:"+food.getIngredients() + "\n"
						+ "辅料："+food.getBurden() + "\n");
				break;
			}
			
			
		};
	};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btn_search.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String menu=et_search.getText().toString().trim();
				new MyThread(menu).start();
			}
		});
	}

	class MyThread extends Thread {
		String menu;

		public MyThread(String menu) {
			this.menu = menu;
		}

		public void run() {
			FinalHttp http = new FinalHttp();
			String url = "http://apis.juhe.cn/cook/query.php";
			AjaxParams params = new AjaxParams();
			params.put("key", "594503d671113a83fc97a4cf2d6b936b");
			params.put("dtype", "json");
			params.put("menu", menu);
			params.put("pn", "");
			params.put("rn", "");
			String result = (String) http.getSync(url, params);
			System.out.println(result);
			if (result != null) {
				try {
					JSONObject object = new JSONObject(result);
					if(object.getString("resultcode").equals("200")){
						Food food = new Food();
						food.setTitle(object.getJSONObject("result").getJSONArray("data").getJSONObject(0).getString("title")
								.toString());
						food.setTags(object.getJSONObject("result").getJSONArray("data").getJSONObject(0).get("tags")
								.toString());
						food.setImtro(object.getJSONObject("result").getJSONArray("data").getJSONObject(0).get("imtro")
								.toString());
						food.setIngredients(object.getJSONObject("result")
								.getJSONArray("data").getJSONObject(0).get("ingredients").toString());
						food.setBurden(object.getJSONObject("result").getJSONArray("data").getJSONObject(0).get("burden")
								.toString());
						food.setAlbums(object.getJSONObject("result").getJSONArray("data").getJSONObject(0).get("albums")
								.toString()); // 图片

						Message msg = new Message();
						msg.what = 1;
						msg.obj = food;
						handler.sendMessage(msg);
					}

				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
